package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ChatRecordMapper;
import com.heima.article.service.ChatRecordService;
import com.heima.model.article.dtos.ChatRecordDto;
import com.heima.model.article.pojos.ChatRecord;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName ChatRecordServiceImpl
 * @Description TODO
 * @Author Neo
 * @Date 2024/3/16 23:22
 * @Version 1.0
 */
@Component
public class ChatRecordServiceImpl extends ServiceImpl<ChatRecordMapper, ChatRecord> implements ChatRecordService {
    @Override
    public ResponseResult chatRecord(ChatRecordDto dto) {
        if(StringUtils.isBlank(dto.getContent())||StringUtils.isBlank(dto.getUserId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setUserId(Long.valueOf(dto.getUserId()));
        chatRecord.setChat(dto.getContent());
        chatRecord.setCreatedTime(new Date());
        boolean save = save(chatRecord);
        if(save){
            return ResponseResult.okResult("保存成功");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    @Override
    public ResponseResult getChatRecord(ChatRecordDto dto) {
        if(StringUtils.isBlank(dto.getUserId())){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        QueryWrapper<ChatRecord> chatRecordQueryWrapper = new QueryWrapper<>();
        chatRecordQueryWrapper.eq("user_id",Long.valueOf(dto.getUserId()));
        chatRecordQueryWrapper.orderByDesc("created_time");
        chatRecordQueryWrapper.last("LIMIT 5");
        List<ChatRecord> list = list(chatRecordQueryWrapper);
        return ResponseResult.okResult(list);
    }
}
